import { defineStore } from 'pinia'

export const useCounterStore = defineStore('counter', {
  state: () => ({
    count: 0,
    history: []
  }),

  getters: {
    doubleCount: (state) => state.count * 2,
    isEven: (state) => state.count % 2 === 0,
    historyCount: (state) => state.history.length
  },

  actions: {
    increment() {
      this.count++
      this.addToHistory('increment')
    },

    decrement() {
      this.count--
      this.addToHistory('decrement')
    },

    reset() {
      this.count = 0
      this.addToHistory('reset')
    },

    incrementBy(amount) {
      this.count += amount
      this.addToHistory(`increment by ${amount}`)
    },

    addToHistory(action) {
      this.history.push({
        action,
        count: this.count,
        timestamp: new Date().toLocaleTimeString()
      })

      if (this.history.length > 10) {
        this.history.shift()
      }
    },

    clearHistory() {
      this.history = []
    }
  }
})
